package vn.appsmobi.ui;

import vn.appsmobi.R;
import vn.appsmobi.activity.DeveloperActivity;
import vn.appsmobi.model.AppInfo;
import vn.appsmobi.ulti.Constant;
import vn.appsmobi.ulti.ImageLoadOption;
import vn.appsmobi.ulti.InstallChecker;
import vn.appsmobi.widget.ImageGalleryView;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

public class AppDetailView extends LinearLayout
{
  private AppInfo mAppInfo;
  private ExpandableTextView mBottomInfo;
  private TextView mBottomInfoTitle;
  private ImageGalleryView  gallery;
 // private HorizontialListView  gallery;
  private View mBottomButtonView;
  private Button mDeveloperButton;
  private Button mRatingButton;
  private TextView mUpdateDateText;
  private TextView mVersionLabel;
  private TextView mVersionText;
  private Context mContext;
  private View.OnClickListener mDeveloperButtonClickListener = new View.OnClickListener()
  {
    public void onClick(View view)
    {
    	Intent intent = new Intent(mContext,DeveloperActivity.class);
      	intent.putExtra(Constant.INTENT_EXTRA_APP_AGENCY, mAppInfo.getCompanyName());
      	intent.putExtra(Constant.INTENT_EXTRA_APP_AGENCY_ID, mAppInfo.getAgencyId());
      	mContext.startActivity(intent);
    }
  };
  
  
  
	
	
  private int mMaxScreenShotHeight;
  private View.OnClickListener mPermissionButtonClickListener = new View.OnClickListener()
  {
    public void onClick(View paramAnonymousView)
    {
      /*if (AppDetailView.this.mAppInfo.permission.isEmpty());
      while (true)
      {
        return;
        Intent localIntent = new Intent(AppDetailView.this.getContext(), PermissionActivity.class);
        localIntent.putExtra("appId", AppDetailView.this.mAppInfo.appId);
        AppDetailView.this.getContext().startActivity(localIntent);
      }
      */
    }
  };
  private View.OnClickListener mCommentEditClickListener = new View.OnClickListener()
  {
    public void onClick(View paramAnonymousView)
    {
    	InstallChecker.showPermissionDialog(mContext,mAppInfo,false);
    }
  };
  
  private TextView mResultNotice;
  
  //private ScreenView mScreenShots;
  


  public AppDetailView(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    this.mContext =  context;
   this.mMaxScreenShotHeight = context.getResources().getDimensionPixelSize(R.dimen.gallery_max_size);
  }

  private void initialize()
  {
   /* this.mTopInfo = ((ExpandableTextView)findViewById(R.id.top_info));
    this.mTopInfo.setMaxLines(12);
    this.mTopInfo.setEllipsize(TextUtils.TruncateAt.END);
    this.mTopInfoTitle = ((TextView)findViewById(R.id.top_info_title));
    */
	this.mBottomButtonView = findViewById(R.id.detail_view_bottom_button_layout);
    this.mRatingButton = ((Button)findViewById(R.id.rating_button));
    this.mRatingButton.setOnClickListener(this.mCommentEditClickListener);
    this.mDeveloperButton = ((Button)findViewById(R.id.developer_button));
    this.mDeveloperButton.setOnClickListener(this.mDeveloperButtonClickListener);
    
    this.mBottomInfo = ((ExpandableTextView)findViewById(R.id.bottom_info));
    this.mBottomInfo.setMaxLines(12);
    this.mBottomInfo.setEllipsize(TextUtils.TruncateAt.END);
    this.mBottomInfoTitle = ((TextView)findViewById(R.id.bottom_info_title));

    this.mUpdateDateText = ((TextView)findViewById(R.id.update_date));
    this.mVersionLabel 	= ((TextView)findViewById(R.id.version_label));
    this.mVersionText 	= ((TextView)findViewById(R.id.version));
    this.mResultNotice 	= ((TextView)findViewById(R.id.result_notice));
    this.gallery 		=  (ImageGalleryView) findViewById(R.id.screen_shots);
  
  }

  protected void onFinishInflate()
  {
    initialize();
  }

  public void updateViewContent(AppInfo appInfo, boolean isupdate)
  {
    Context ctx;
    if (!TextUtils.isEmpty(appInfo.getId()))
    {
      ctx = getContext();
      this.mAppInfo = appInfo;
      String emptyString = ctx.getString(R.string.empty_string);
      if(TextUtils.isEmpty(this.mAppInfo.getDetail()))
      {
    	  this.mBottomInfo.setText(emptyString);
      }
      else
    	  this.mBottomInfo.setText(Html.fromHtml(this.mAppInfo.getDetail()) ); 
           
 
      this.mVersionText.setText(this.mAppInfo.getVersion());
      
      
      if (appInfo.getImages() != null && appInfo.getImages().size() > 0)
	  { 
    	
    	 
		 for(int i =0; i< appInfo.getImages().size(); i++)
		  {
			  final ImageSwitcher imgSwitcher = new ImageSwitcher(ctx);
	          imgSwitcher.setMinimumHeight(this.mMaxScreenShotHeight);
	          //imgSwitcher.setFactory(this.mScreenShotViewFactory);  
	          imgSwitcher.setInAnimation(AnimationUtils.loadAnimation(AppDetailView.this.mContext, android.R.anim.fade_in));             
	          imgSwitcher.setOutAnimation(AnimationUtils.loadAnimation(AppDetailView.this.mContext,android.R.anim.fade_out));
	          ImageLoader.getInstance().loadImage(appInfo.getImages().get(i),ImageLoadOption.options, new ImageLoadingListener() {
	    			
	    			@Override
	    			public void onLoadingStarted(String arg0, View arg1) {
	    				// TODO Auto-generated method stub
	    				((ImageView)arg1).setImageResource(R.drawable.place_holder_screen);
	    			}
	    			
	    			@Override
	    			public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
	    				// TODO Auto-generated method stub
	    				((ImageView)arg1).setImageResource(R.drawable.place_holder_screen);
	    			}
	    			
	    			@Override
	    			public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
	    				// TODO Auto-generated method stub	    				
	    				Drawable drawable = new BitmapDrawable(arg2);
	      				int originHeight = drawable.getIntrinsicHeight();
	      				int originWidth  = drawable.getIntrinsicWidth();
	      				
	      				final int newHeight = AppDetailView.this.mMaxScreenShotHeight;
	      				float scale   = newHeight/originHeight;
	      				final int newWidth  = Math.round(scale*originWidth);
	      				ViewSwitcher.ViewFactory mScreenShotViewFactory = new ViewSwitcher.ViewFactory()
		      			  {
		      			    public View makeView()
		      			    {
		      			      ImageView imgview = new ImageView(AppDetailView.this.getContext());
		      			      FrameLayout.LayoutParams localparam = new FrameLayout.LayoutParams(newWidth,newHeight);
		      			      localparam.setMargins(5, 0, 5, 0);
		      			      imgview.setScaleType(ImageView.ScaleType.FIT_CENTER);
		      			      localparam.gravity = Gravity.TOP;
		      			      imgview.setLayoutParams(localparam);		      			      
		      			      imgview.setMaxHeight(AppDetailView.this.mMaxScreenShotHeight);
		      			      imgview.setAdjustViewBounds(true);
		      			      return imgview;
		      			    }
		      			  };
		      			imgSwitcher.setFactory(mScreenShotViewFactory);  
	      				//LinearLayout.LayoutParams localparam = new LinearLayout.LayoutParams(newWidth,newHeight);
    	      			//imgSwitcher.setLayoutParams(localparam);
	    				imgSwitcher.setImageDrawable(drawable);
	    				
	    			}
	    			
	    			@Override
	    			public void onLoadingCancelled(String arg0, View arg1) {
	    				// TODO Auto-generated method stub
	    				
	    			}
	    		});
	          
	          this.gallery.addImage(imgSwitcher);
		  }
		  
    	  
    	
	  }
     
         
    }
  }
  
  public Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

      Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),

          bitmap.getHeight(), Config.ARGB_8888);

      Canvas canvas = new Canvas(output);

   
      final int color = 0xff424242;

      final Paint paint = new Paint();

      final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

      final RectF rectF = new RectF(rect);

      final float roundPx = 12;

   
      paint.setAntiAlias(true);

      canvas.drawARGB(0, 0, 0, 0);

      paint.setColor(color);
      canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

   
      paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

      canvas.drawBitmap(bitmap, rect, rect, paint);

   
      return output;
    }
}

